package com.howlaisi.sms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.howlaisi.sms.entity.Student;
import com.howlaisi.sms.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/students")
public class StudentController {
    
    @Autowired
    private StudentService studentService;
    
    @PostMapping
    public Student create(@RequestBody Student student) {
        studentService.save(student);
        return student;
    }
    
    @PutMapping("/{id}")
    public Student update(@PathVariable Long id, @RequestBody Student student) {
        student.setId(id);
        studentService.updateById(student);
        return student;
    }
    
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id) {
        return studentService.removeById(id);
    }
    
    @GetMapping("/{id}")
    public Student getById(@PathVariable Long id) {
        return studentService.getById(id);
    }
    
    @GetMapping
    public Page<Student> list(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer size,
                            @RequestParam(required = false) String name,
                            @RequestParam(required = false) String studentNo) {
        Page<Student> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        
        if (name != null && !name.isEmpty()) {
            queryWrapper.like(Student::getName, name);
        }
        if (studentNo != null && !studentNo.isEmpty()) {
            queryWrapper.eq(Student::getStudentNo, studentNo);
        }
        
        return studentService.page(pageParam, queryWrapper);
    }
} 